package kadledav.adventure.framework.builder.name;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kadledav.adventure.framework.model.general.ActionObject;
import kadledav.adventure.framework.model.world.Item;
import kadledav.adventure.framework.model.world.Location;
import kadledav.adventure.framework.model.world.impl.PassageBasic;

class SimpleNameLockedPassage extends PassageBasic {

	public SimpleNameLockedPassage(String name, Location pointedLocation) {
		super(name, "", pointedLocation);
	}

	private boolean lock = true;
	private Map<String, String> unlockItems = new HashMap<String, String>();
	protected PassageBasic otherSide = null;

	public void setOtherSide(PassageBasic actionObject) {
		otherSide = actionObject;
	}

	protected void doActionOnOtherSide(ActionObject actionItem, List<Item> inventory) {
		if (otherSide != null && otherSide.getPointedLocation()==null) {
			otherSide.doActionWith(actionItem, inventory);
		}
	}

	public void addUnlockItem(String intemName, String newPassageName) {
		unlockItems.put(intemName, newPassageName);
	}

	@Override
	public Location getPointedLocation() {
		if (lock) {
			return null;
		}
		return super.getPointedLocation();
	}

	@Override
	public String doActionWith(ActionObject actionItem, List<Item> inventory) {
		String newPassageName = unlockItems.get(actionItem.getName());
		if (newPassageName == null) {
			return null;
		}
		lock = false;
		doActionOnOtherSide(actionItem, inventory);
		name = newPassageName;
		return "Cesta je volna. Novy nazev pruchodu je " + newPassageName;
	}
}
